package gateway.listener;

import cn.hutool.core.bean.BeanUtil;
import com.yymt.bus.cms.business.gateway.GatewayAccessService;
import com.yymt.bus.cms.business.gateway.model.AccessApiVo;
import com.yymt.comp.base.utils.ConvertUtil;
import com.yymt.comp.gateway.event.GatewayAccessEvent;
import com.yymt.comp.sys.SysLogService;
import com.yymt.comp.sys.persistence.SysLogEntity;
import com.yymt.comp.sys.persistence.SysUserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author liuhuagui
 * @version 1.0.0
 * @date 2024-08-20 17:32:38
 */
@Component
@Slf4j
public class GatewayAccessListener {

    @Resource
    private GatewayAccessService gatewayAccessService;

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private SysUserDao sysUserDao;

    @Async
    @EventListener
    public void handleEvent(GatewayAccessEvent event) {
        log.info("监听到网关访问事件:{}", event);
        // TODO 暂时收集数据用
        gatewayAccessService.saveGatewayAccessApi(event.getUri(), event.getRequestMethod());

        // 记录日志
        AccessApiVo gatewayAccessApi = gatewayAccessService.findGatewayAccessApi(event.getUri(), event.getRequestMethod());
        // 记录日志
        if (gatewayAccessApi == null || !gatewayAccessApi.isNeedLog()){
            return;
        }
        SysLogEntity sysLogEntity = new SysLogEntity();
        sysLogEntity.setIp(event.getIp());
        sysLogEntity.setMethod(event.getRequestMethod());
        sysLogEntity.setModelId(gatewayAccessApi.getModuleId());
        sysLogEntity.setModelName(gatewayAccessApi.getModuleName());
        sysLogEntity.setOperation(gatewayAccessApi.getAction());
        // 设置IP地址
        // 用户名
        Map<String, Object> userMap = BeanUtil.beanToMap(event.getUserInfo());
        sysLogEntity.setUsername(ConvertUtil.parseEmptyStr(userMap.get("username")));
        long userId = ConvertUtil.parseLong(userMap.get("userId"));
        sysLogEntity.setTime(event.getTime());
        sysLogEntity.setCreateDate(new Date());

        Map<String, Object> map = sysUserDao.queryUserInfo(userId);
        String realname = "";
        String username = "";
        String deptName = "";
        int deptId = 0;
        if (map != null) {
            realname = ConvertUtil.parseEmptyStr(map.get("realname"));
            username = ConvertUtil.parseEmptyStr(map.get("username"));
            deptName = ConvertUtil.parseEmptyStr(map.get("deptName"));
            deptId = ConvertUtil.parseInt(map.get("deptId"));
        }
        sysLogEntity.setRealname(realname);
        sysLogEntity.setDeptName(deptName);
        sysLogEntity.setDeptId(deptId);
        sysLogEntity.setUsername(username);
        sysLogEntity.setUserId(userId);
        // 保存系统日志
        sysLogService.save(sysLogEntity);
    }
}
